package deibay.client.core;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.logging.Level;

import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import deibay.DeibayLogger;
import deibay.client.Controller;
import deibay.client.IModel;
import deibay.client.core.views.IView;
import deibay.messages.RequestItemsListMsg;
import deibay.messages.RequestSellerItemsListMsg;

public class DeibayMainController extends Controller {	
		
	public DeibayMainController(IView view, IModel model) {
		super(view, model);
	}

	@Override
	public void initView() 
	{
		view.setLogoutItemDisabled();		
		showMessage = new ShowMessageView();
	}

	@Override
	public void initModel() {			
	}

	@Override
	public void createViewEvents() {

		view.setLoginItemAction(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				showLoginView();
			}

		});

		view.setRegisterItemAction(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				showRegisterView();
			}

		});
		
		view.setLogoutItemAction(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {				
				logout();
			}						
		});
		
		view.setWindowClose(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e) {    				
					logout();
					DeibayLogger.getInstance().getLog().log(Level.WARNING, "A aplicacao cliente foi fechada.");
					System.exit(0);
				}			
		});
		
		view.getPanel().setSelectedTabChangesAction(new ChangeListener()
		{
			public void stateChanged(ChangeEvent arg0) {				
				JTabbedPane pane = (JTabbedPane)arg0.getSource();
				    
  	            int sel = pane.getSelectedIndex();
  	            if(sel == 0)
  	            {  	   
  	            	if(model.getLogin()!=null)
  	            		model.sendMessage(new RequestItemsListMsg(RequestItemsListMsg.class.getSimpleName()));
  	            }
  	            else if(sel == 1)
  	            {
  	            	if(model.getLogin()!=null)
  	            		if(model.getLogin().getRole().equals("seller"))
  	            		model.sendMessage(new RequestSellerItemsListMsg(RequestSellerItemsListMsg.class.getSimpleName()));
  	            }
			}
			
		});
				
	}

	public void showLoginView()
	{
		loginView = view.startLoginView();
		loginView.setLoginButtonAction(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				model.setMessageConsumer(new MessageConsumer(DeibayMainController.this));				
				model.setLogin(new Login(model, DeibayMainController.this, loginView.getUsername(), loginView.getPassword()));
				if(model.getLogin().getCommunicator()!=null)
					login();																		
											
			}

		});
	}
	
	public void showRegisterView()
	{
		registerView = view.startRegisterView(); 
		registerView.setRegisterButtonAction(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				model.setMessageConsumer(new MessageConsumer(DeibayMainController.this));
				model.setLogin(new Login(model, DeibayMainController.this, registerView.getUsername(), registerView.getPassword()));
				if(model.getLogin().getCommunicator()!=null)
					register();																				
			}
		});
	}				
	
	@Override
	public void login()
	{
		setLoggedState();
		model.getLogin().initializeThreads();
		model.getLogin().sendLogin();
		loginView.close();
		loginView = null;		
	}
	
	@Override
	public void register()
	{
		setLoggedState();
		model.getLogin().initializeThreads();
		model.getLogin().sendRegister(registerView.getEmail(), registerView.getAddress(), registerView.getRole());
		registerView.close();
		registerView = null;		
	}
	
	@Override
	public void logout() {			
		setLoggoutState();
		model.closeCommunicator();
	}

	@Override
	public void logged() {			
		this.showMessage.showSucessfull("Login", "Sucesso.");
		model.sendMessage(new RequestItemsListMsg(RequestItemsListMsg.class.getSimpleName()));
		view.getPanel().setRole(model.getLogin().getRole());		
		setBidderController();
		if(model.getLogin().getRole().equals("seller"))
		{
			setSellerController();
			getSellerController().getSellerView().setAddItemActionEnabled(true);
			getSellerController().getSellerView().setSendAdvertiseActionEnabled(true);
		}
	}

	@Override
	public void setBidderController() {
		this.bidderController = new DeibayBidderController(view.getPanel().showBidderView(), model);
	}

	@Override
	public void setSellerController() {
		this.sellerController = new DeibaySellerController(view.getPanel().showSellerView(), model);		
	}

	@Override
	public void setLoggedState() {
		view.setLoginItemDisabled();
		view.setRegisterItemDisabled();
		view.setLogoutItemEnabled();		
	}

	@Override
	public void setLoggoutState() {
		view.setLoginItemEnabled();
		view.setRegisterItemEnabled();
		view.setLogoutItemDisabled();		
	}
}
